<?php

namespace app\api\controller;

use app\admin\model\Account;
use app\admin\model\Favor;
use app\admin\model\Post;
use app\admin\model\Gcw;

/**
 * @author zero
 * @Time 2019/2/12
 *
 * Class Accounts
 * @package app\api\controller
 *
 * 悦图籍和天天广场舞教学用户接口
 */
class Accounts extends Base
{

    public function login()
    {
        $appid = '';
        $secret = '';
        $from_type = $this->request->post('from_type');
        $code = $this->request->post('code');

        switch ($from_type) {
            case 'gcw':
                $appid = 'wx77a9968ac2c2d9e7';
                $secret = '68891bc7ac5f5300eeeb3f6c418f8617';
                break;
            case 'ytj':
                $appid = 'wxa626926803a7a03b';
                $secret = 'c013140e341ebfc9bdcf93b0c0d4951f';
                break;
        }

        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $params['appid'] = $appid;
        $params['secret'] = $secret;
        $params['js_code'] = $code;
        $params['grant_type'] = 'authorization_code';

        $res = $this->http($url, $params);
        $json = json_decode($res, true);
        $msg = $json['openid'];

        if ($account = Account::get(['openid' => $json['openid']])) {
            $data = $account->id;
        } else {
            $a = new Account;
            $a->openid = $json['openid'];
            $a->save();
            $data = $a->id;
        }

        $this->result($msg, $data);
    }

    public function config($v)
    {
        $videoflag = true;
        $adflag = true;
        $xcxflag = true;

        if ($v == '0.9.9') {
            $videoflag = false;
            $adflag = false;
            $xcxflag = false;
        }

        $flags = array(
            'videoflag' => $videoflag,
            'adflag' => $adflag,
            'xcxflag' => $xcxflag,
        );

        $this->result('flag', $flags);
    }

    public function fetch_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $favor_type = $this->request->post('collect_type');
        $account_id = $this->request->post('user_id');

        if ($f = Favor::get([
            'account_id' => $account_id,
            'favor_type' => $favor_type,
            'favor_id' => $favor_id
        ])) {
            $msg = 'ok';
        } else {
            $msg = 'false';
        }
        $this->result($msg);
    }

    public function add_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $favor_type = $this->request->post('collect_type');
        $account_id = $this->request->post('user_id');

        $f = new Favor;
        $f->data([
            'account_id' => $account_id,
            'favor_type' => $favor_type,
            'favor_id' => $favor_id
        ]);
        $f->save();
        $this->success('ok');
    }

    public function del_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $favor_type = $this->request->post('collect_type');
        $account_id = $this->request->post('user_id');

        $f = Favor::get([
            'account_id' => $account_id,
            'favor_type' => $favor_type,
            'favor_id' => $favor_id
        ]);
        if ($f) {
            $f->delete();
        }
        $this->success('ok');
    }

    public function get_favor_list() {
        $favor_type = $this->request->post('collect_type');
        $account_id = $this->request->post('user_id');
        $list = array();

        if ($favor_type == 'post') {
            $favor_list = Favor::where(['account_id' => $account_id, 'favor_type' => 'post'])->column('favor_id, createtime');
            foreach ($favor_list as $id => $time) {
                $post = Post::get($id);
                $post->createtime = date('Y年m月d日 h:m', $time);
                array_push($list, $post);
            }
        }

        if ($favor_type == 'gcw') {
            $favor_list = Favor::where(['account_id' => $account_id, 'favor_type' => 'video'])->column('favor_id, createtime');
            foreach ($favor_list as $id => $time) {
                $item = Gcw::get($id);
                $item->createtime = date('Y年m月d日', $time);
                array_push($list, $item);
            }
        }

        $this->result('list', array_reverse($list));
    }

}
